package home.my.recordmanage.service;

import home.my.base.service.BaseService;
import home.my.recordmanage.dao.StyleDao;
import home.my.recordmanage.model.Style;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

public class StyleService extends BaseService<Style> {

	//列jqueryui的样式列表
	public List<String> listAllStylesFromFileSystem(String appRoot) {
		List<String> styleList = new ArrayList<String>();
		String fileName = appRoot + "/css/";
		File cssDir = new File(fileName);
		if(cssDir.exists() && cssDir.isDirectory()){
			
			File[] dirs = cssDir.listFiles(new FileFilter() {
				@Override
				public boolean accept(File pathname) {
					boolean isDir = pathname.isDirectory();
					if(!isDir)
						return false;
					File[] subFiles = pathname.listFiles();
					if(subFiles!=null && subFiles.length>0){
						for(File subFile:subFiles){
							if(subFile.getName().equals("jquery-ui.css")){
								return true;
							}
						}
					}
					return false;
				}
			});
			
			if(dirs!=null && dirs.length>0){
				for(File dir:dirs){
					styleList.add(dir.getName());
				}
			}
		}
		return styleList;
	}

	//保存当前使用的样式到db
	public void saveCurrentStyleToDb(String styleName) {
		((StyleDao)this.getDao()).saveCurrentStyleToDb(styleName);
	}
}